// ----------------------------------
// RSDK Project: Sonic 1/Sonic 2
// Script Description: Button Pillar Object
// Script Author: Christian Whitehead/Simon Thomley
// Unpacked by Rubberduckycooly's script unpacker
// ----------------------------------

//-------Aliases-------//
private alias 64 : TYPE_BUTTONPILLAR

// Function declarations
reserve function ButtonPillar_DebugDraw
reserve function ButtonPillar_DebugSpawn
reserve function ButtonPillar_PushMoniUp
reserve function ButtonPillar_PushMoniUpandDown

// Static Values

// Tables

function ButtonPillar_DebugDraw
	DrawSprite(0)
end function


function ButtonPillar_DebugSpawn
	CreateTempObject(TypeName[Button Pillar], 0, object.xpos, object.ypos)
	object[tempObjectPos].value1 = object.ypos
end function


function ButtonPillar_PushMoniUp
	object[currentPlayer].iypos = object[currentPlayer].collisionTop
	FlipSign(object[currentPlayer].ypos)
	object[currentPlayer].ypos -= 0x500000
	object[currentPlayer].ypos += object.ypos
	if object[currentPlayer].yvel > 0
		object[currentPlayer].yvel = 0
	end if
end function

event ObjectMain
	switch object.state
	case 0
		if object[-1].propertyValue == 1
			object.state = 1
			object.priority = PRIORITY_ACTIVE
		end if
		foreach (GROUP_PLAYERS, currentPlayer, ACTIVE_ENTITIES)
			CheckEqual(object[0].state, PlayerObject_HandleMonikaTeleport)
			temp7 = checkResult
			CheckLower(object[0].value1, 20)
			temp7 &= checkResult
			if temp7 == 0				
				BoxCollisionTest(C_BOX, object.entityPos, -32, -56, 32, 56, currentPlayer, HITBOX_AUTO, HITBOX_AUTO, HITBOX_AUTO, HITBOX_AUTO)
			end if	
			if object[currentPlayer].state == PlayerObject_HandleMonikaTeleport
				if object[0].value1 >= 14
					BoxCollisionTest(C_TOUCH, object.entityPos, -32, -56, 32, 56, currentPlayer, HITBOX_AUTO, HITBOX_AUTO, HITBOX_AUTO, HITBOX_AUTO)
					if checkResult == 1
						CallFunction(ButtonPillar_PushMoniUp)
					end if
				end if
			end if				
		next
		break
	case 1
		if object.value0 < 144
			object.value0 += 2
			foreach (GROUP_PLAYERS, currentPlayer, ACTIVE_ENTITIES)
				CheckEqual(object[0].state, PlayerObject_HandleMonikaTeleport)
				temp7 = checkResult
				CheckLower(object[0].value1, 20)
				temp7 &= checkResult
				if temp7 == 0				
					BoxCollisionTest(C_BOX, object.entityPos, -32, -56, 32, 56, currentPlayer, HITBOX_AUTO, HITBOX_AUTO, HITBOX_AUTO, HITBOX_AUTO)
					if checkResult == 1
						object[currentPlayer].ypos += 0x20000
					end if
				end if	
				if object[currentPlayer].state == PlayerObject_HandleMonikaTeleport
					if object[0].value1 >= 14
						BoxCollisionTest(C_TOUCH, object.entityPos, -32, -56, 32, 56, currentPlayer, HITBOX_AUTO, HITBOX_AUTO, HITBOX_AUTO, HITBOX_AUTO)
						if checkResult == 1
							if object[currentPlayer].ypos > object.ypos
								CallFunction(ButtonPillar_PushMoniUp)
							else
								CallFunction(ButtonPillar_PushMoniUp)
							end if
						end if
					end if
				end if					
			next
			object.ypos += 0x20000
		else
			foreach (GROUP_PLAYERS, currentPlayer, ACTIVE_ENTITIES)
				CheckEqual(object[0].state, PlayerObject_HandleMonikaTeleport)
				temp7 = checkResult
				CheckLower(object[0].value1, 20)
				temp7 &= checkResult
				if temp7 == 0				
					BoxCollisionTest(C_BOX, object.entityPos, -32, -56, 32, 56, currentPlayer, HITBOX_AUTO, HITBOX_AUTO, HITBOX_AUTO, HITBOX_AUTO)
				end if	
				if object[currentPlayer].state == PlayerObject_HandleMonikaTeleport
					if object[0].value1 >= 14
						BoxCollisionTest(C_TOUCH, object.entityPos, -32, -56, 32, 56, currentPlayer, HITBOX_AUTO, HITBOX_AUTO, HITBOX_AUTO, HITBOX_AUTO)
						if checkResult == 1
							CallFunction(ButtonPillar_PushMoniUp)
						end if
					end if		
				end if					
			next
			object.state++
		end if
		break
	case 2
		foreach (GROUP_PLAYERS, currentPlayer, ACTIVE_ENTITIES)
			CheckEqual(object[0].state, PlayerObject_HandleMonikaTeleport)
			temp7 = checkResult
			CheckLower(object[0].value1, 20)
			temp7 &= checkResult
			if temp7 == 0				
				BoxCollisionTest(C_BOX, object.entityPos, -32, -56, 32, 56, currentPlayer, HITBOX_AUTO, HITBOX_AUTO, HITBOX_AUTO, HITBOX_AUTO)
			end if	
			if object[currentPlayer].state == PlayerObject_HandleMonikaTeleport
				if object[0].value1 >= 14
					BoxCollisionTest(C_TOUCH, object.entityPos, -32, -56, 32, 56, currentPlayer, HITBOX_AUTO, HITBOX_AUTO, HITBOX_AUTO, HITBOX_AUTO)
					if checkResult == 1
						CallFunction(ButtonPillar_PushMoniUp)
					end if
				end if
			end if				
		next
		break
	end switch	
	object.angle = oscillation
	object.angle <<= 1
	Sin(object.value2, object.angle)
	object.value2 <<= 12
	object.value2 += object.ypos
	object.value2 &= -0x10000
	if object.outOfBounds == 1
		temp0 = object.ypos
		object.ypos = object.value1
		if object.outOfBounds == 1
			object.priority = PRIORITY_ACTIVE_BOUNDS
			object.state = 0
			object.value0 = 0
		else
			object.ypos = temp0
		end if
	end if
end event


event ObjectDraw
	DrawSprite(0)
	DrawSpriteXY(1, object.xpos, object.value2)
end event


event ObjectStartup
	LoadSpriteSheet("MZ/Objects.gif")
	SpriteFrame(-32, -56, 64, 112, 126, 1)
	SpriteFrame(-16, -16, 31, 32, 159, 114)
	foreach (TypeName[Button Pillar], arrayPos0, ALL_ENTITIES)
		object[arrayPos0].value1 = object[arrayPos0].ypos
	next
	SetTableValue(TypeName[Button Pillar], DebugMode_ObjCount, DebugMode_TypesTable)
	SetTableValue(ButtonPillar_DebugDraw, DebugMode_ObjCount, DebugMode_DrawTable)
	SetTableValue(ButtonPillar_DebugSpawn, DebugMode_ObjCount, DebugMode_SpawnTable)
	DebugMode_ObjCount++
end event

event RSDKDraw
	DrawSprite(0)
end event

event RSDKLoad
	LoadSpriteSheet("Global/Display.gif")
	SpriteFrame(-16, -16, 32, 32, 1, 143)
end event
